package fox.framework.rpc.common;


import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Enumeration;

/**
 * @author cuichao
 * @Description: WebUtils
 * @Date: create in 2020/12/29 22:39
 */
public class WebUtils {

    private static final String DEFAULT_CHAR_SET = "UTF-8";

    /**
     * 获取本机第一张网卡的IP地址
     *
     * @return
     */
    public static String getDefaultInetHost() {
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            if (!netInterfaces.hasMoreElements()) {
                return null;
            }
            NetworkInterface networkInterface = netInterfaces.nextElement();
            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            if (!addresses.hasMoreElements()) {
                return null;
            }
            InetAddress inetAddress = addresses.nextElement();
            return inetAddress.getHostAddress();

        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String urlDecode(String url) {
        try {
            return URLDecoder.decode(url,DEFAULT_CHAR_SET);
        } catch (UnsupportedEncodingException e) {
           return null;
        }
    }


    public static String urlEncode(String url){
        try {
           return URLEncoder.encode(url, DEFAULT_CHAR_SET);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}
